﻿using ZOIL.Framework.Presentation;
using System.Windows;

namespace ZOIL.Library.Behaviors
{
    public delegate void ViewportEventHandler(object sender, ViewportEventArgs e);

    public class SynchronizeViewportBehavior : RuntimeBehavior<ZInformationLandscape, SynchronizeViewportPluginBehavior>
    {
        #region dependency properties

        #region Type

        public CommunicationType Type
        {
            get { return (CommunicationType)GetValue(TypeProperty); }
            set { SetValue(TypeProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Type.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TypeProperty =
            DependencyProperty.Register("Type", typeof(CommunicationType), typeof(SynchronizeViewportBehavior), new PropertyMetadata(CommunicationType.Both, OnTypeChanged));

        /// <summary>
        /// Handles changes to the Type property.
        /// </summary>
        private static void OnTypeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var target = (SynchronizeViewportBehavior)d;
            var oldType = (CommunicationType)e.OldValue;
            var newType = target.Type;
            target.OnTypeChanged(oldType, newType);
        }

        /// <summary>
        /// Provides derived classes an opportunity to handle changes to the Type property.
        /// </summary>
        protected virtual void OnTypeChanged(CommunicationType oldType, CommunicationType newType)
        {
        }

        #endregion

        #region SenderID

        /// <summary>
        /// SenderID Dependency Property
        /// </summary>
        public static readonly DependencyProperty SenderIDProperty =
            DependencyProperty.Register("SenderID", typeof(string), typeof(SynchronizeViewportBehavior),
                new FrameworkPropertyMetadata(null, OnSenderIDChanged));

        /// <summary>
        /// Gets or sets the SenderID property. This dependency property indicates ....
        /// </summary>
        public string SenderID
        {
            get { return (string)GetValue(SenderIDProperty); }
            set { SetValue(SenderIDProperty, value); }
        }

        /// <summary>
        /// Handles changes to the SenderID property.
        /// </summary>
        private static void OnSenderIDChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var target = (SynchronizeViewportBehavior)d;
            var oldSenderID = (string)e.OldValue;
            var newSenderID = target.SenderID;
            target.OnSenderIDChanged(oldSenderID, newSenderID);
        }

        /// <summary>
        /// Provides derived classes an opportunity to handle changes to the SenderID property.
        /// </summary>
        protected virtual void OnSenderIDChanged(string oldSenderID, string newSenderID)
        {
        }

        #endregion 

        #endregion

        #region routed events

        #region Viewport Received

        public static readonly RoutedEvent ViewportReceivedEvent = EventManager.RegisterRoutedEvent("ViewportReceived", RoutingStrategy.Bubble, typeof(ViewportEventHandler), typeof(SynchronizeViewportBehavior));

        public event ViewportEventHandler ViewportReceived
        {
            add { AddRoutedEventHandler(ViewportReceivedEvent, value); }
            remove { RemoveRoutedEventHandler(ViewportReceivedEvent, value); }
        }

        #endregion

        #endregion

        #region event helper

        internal void RaiseViewportReceivedEvent(Viewport viewport)
        {
            var args = new ViewportEventArgs(ViewportReceivedEvent, viewport) { Source = this };
            AssociatedObject.RaiseEvent(args);
        }

        #endregion
    }
}
